package com.wan37.logic.skill.impl;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.AllRoleVO;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.handler.WorkerHandler;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.skill.AbstractEnemyAOESkill;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.SkillLabel;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-07-22 11:21
 **/
@Component(SkillLabel.AOE + SkillLabel.ENEMY + SkillLabel.IRONY)
@Scope("prototype")
public class IronySkillEnemy extends AbstractEnemyAOESkill {
    @Override
    public Map<String, Map<String, Long>> handle(String sceneId) {
        List<RoleVO> roleVOList = new ArrayList<>();
        allRoleVOList.forEach(allRoleVO1 -> {
            if (allRoleVO1.getAttackEntityId() == null) {
                allRoleVO1.setAttackEntityId(new ArrayList<>());
            }
            /**
             * 嘲讽技能导致添加攻击对象
             */
            allRoleVO1.getAttackEntityId().add(allRoleVO.getAllRoleId());
            if (allRoleVO1.getLabel() == AllRoleLabel.ROLE) {
                roleVOList.add((RoleVO) allRoleVO1);
            } else if (allRoleVO1.getLabel() == AllRoleLabel.MONSTER) {
                routeUtils.sendAoiBroadcastResponse(Common.IRONY_USE,
                        sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet(), allRoleVO1);
            }
        });
        if (!roleVOList.isEmpty()) {
            routeUtils.sendAoiRoleBroadcastResponse(Common.IRONY_USE, sceneServiceCache
                    .getSceneServiceMap().get(sceneId).getUserIdSet(), roleVOList);
        }
        delayDequeThreadPollUtil.updateTimeSchedule(skillVO.getDuration(), allRoleVO.getAllRoleId(), sceneId, new Fire(sceneId));
        return new HashMap<>();
    }

    class Fire implements WorkerHandler {
        String sceneId;

        public Fire(String sceneId) {
            this.sceneId = sceneId;
        }

        @Override
        public void execute(Worker worker) throws Exception {
            if (sceneServiceCache.getSceneServiceMap().containsKey(sceneId)) {
                List<RoleVO> roleVOList = new ArrayList<>();
                allRoleVOList.forEach(allRoleVO1 -> {
                    allRoleVO1.getAttackEntityId().remove(allRoleVO.getAllRoleId());
                    if (allRoleVO1.getLabel() == AllRoleLabel.ROLE) {
                        roleVOList.add((RoleVO) allRoleVO1);
                    } else if (allRoleVO1.getLabel() == AllRoleLabel.MONSTER) {
                        routeUtils.sendAoiBroadcastResponse(Common.IRONY_CANCEL,
                                sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet(), allRoleVO1);
                    }
                });
                if (!roleVOList.isEmpty()) {
                    routeUtils.sendAoiRoleBroadcastResponse(Common.IRONY_USE, sceneServiceCache
                            .getSceneServiceMap().get(sceneId).getUserIdSet(), roleVOList);
                }
            }
        }
    }
}
